package com.oceansoft.mobile.econsole.modules.situation.controller;

import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.situation.entity.Situation;
import com.oceansoft.mobile.econsole.modules.situation.service.ISituationService;
import com.oceansoft.mobile.econsole.modules.sys.entity.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;


/**
 * Created by 我的大名叫陆健伟 on 2016/2/17.
 */
@Controller
@RequestMapping(value = "situation")
public class SituationController extends BaseController {

    @Resource
    private ISituationService situationService;

    @RequestMapping(value = "index")
    public String gotoSituationIndex(Query query, Model model){
        Pager<Situation> pager = situationService.loadSituationsByPage(query);
        if(pager.getList() != null){
            for(int i = 0 ; i < pager.getList().size() ; i++){
                pager.getList().get(i).setArea(Constant.SITUATION_AREA.get(pager.getList().get(i).getArea()));
                pager.getList().get(i).setHighwaySituation(Constant.SITUATION_HIGHWAY.get(pager.getList().get(i).getHighwaySituation()));
            }
        }
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log("查看高速路况列表");
        return "situation/situation-list";
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public String loadPageByCondition(Query query, Model model){
        Pager<Situation> pager = situationService.loadSituationsByPage(query);
        if(pager.getList() != null){
            for(int i = 0 ; i < pager.getList().size() ; i++){
                pager.getList().get(i).setArea(Constant.SITUATION_AREA.get(pager.getList().get(i).getArea()));
                pager.getList().get(i).setHighwaySituation(Constant.SITUATION_HIGHWAY.get(pager.getList().get(i).getHighwaySituation()));
            }
        }
        if(pager.getList().size() == 0){
            model.addAttribute("noData",1);
        }
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log(String.format("查看高速路况第%dÎ页", query.getPageNum()));
        return "situation/situation-list";
    }

    @RequestMapping(value = "gotoSituation/{situation_id}", method = RequestMethod.GET)
    public String gotoSituation(@PathVariable("situation_id")String situation_id, Model model){
        SysUser sysUser = getSession();
        if(situation_id.equals("0")){
            model.addAttribute("isAddSituation", 0);
            model.addAttribute("situation", new Situation());
        }else {
            Situation situation = situationService.loadSituationById(situation_id);
            model.addAttribute("situation", situation);
            model.addAttribute("isAddSituation", 1);
        }
        model.addAttribute("loginUserOrg", sysUser.getOrgName());
        return "situation/situation-edit";
    }

    @ResponseBody
    @RequestMapping(value = "addSituation", method = RequestMethod.POST)
    public Result addSituation(Situation situation, HttpServletRequest request){
        String isAddSituation = request.getParameter("isAddSituation");
        if(isAddSituation.equals("0")){
            situation.setId(UUID.randomUUID().toString());
            return situationService.savaSituation(situation);
        }
        else {
            return situationService.updateSituation(situation);
        }
    }

    @ResponseBody
    @RequestMapping(value = "deleteSituation/{situationId}", method = RequestMethod.GET)
    public Result deleteSituation(@PathVariable("situationId")String situationId){
        return situationService.deleteSituation(situationId);
    }


}
